
<template>
  <div>
    <div class="gva-form-box">
      <el-form :model="formData" ref="elFormRef" label-position="right" :rules="rule" label-width="80px">
        <el-form-item label="就诊卡号:" prop="patientCardNum">
          <el-input v-model.number="formData.patientCardNum" :clearable="true" placeholder="请输入" />
       </el-form-item>
        <el-form-item label="患者姓名:" prop="name">
          <el-input v-model="formData.name" :clearable="true"  placeholder="请输入患者姓名" />
       </el-form-item>
        <el-form-item label="性别:" prop="sex">
           <el-select v-model="formData.sex" placeholder="请选择性别" style="width:100%" :clearable="true" >
              <el-option v-for="(item,key) in genderOptions" :key="key" :label="item.label" :value="item.value" />
           </el-select>
       </el-form-item>
        <el-form-item label="手机号码:" prop="mobile">
          <el-input v-model="formData.mobile" :clearable="true"  placeholder="请输入手机号码" />
       </el-form-item>
        <el-form-item label="患者头像:" prop="patientImage">
          <el-input v-model="formData.patientImage" :clearable="true"  placeholder="请输入患者头像" />
       </el-form-item>
        <el-form-item label="是否有遗传病史:" prop="haveHastile">
           <el-select v-model="formData.haveHastile" placeholder="请选择是否有遗传病史" style="width:100%" :clearable="true" >
              <el-option v-for="(item,key) in GeneticDiseaseOptions" :key="key" :label="item.label" :value="item.value" />
           </el-select>
       </el-form-item>
        <el-form-item label="证件类型:" prop="cardType">
           <el-select v-model="formData.cardType" placeholder="请选择证件类型" style="width:100%" :clearable="true" >
              <el-option v-for="(item,key) in CardTypeOptions" :key="key" :label="item.label" :value="item.value" />
           </el-select>
       </el-form-item>
        <el-form-item label="出生年月:" prop="borthid">
          <el-date-picker v-model="formData.borthid" type="date" placeholder="选择日期" :clearable="true"></el-date-picker>
       </el-form-item>
        <el-form-item label="年龄:" prop="age">
          <el-input v-model.number="formData.age" :clearable="true" placeholder="请输入" />
       </el-form-item>
        <el-form-item label="民族:" prop="people">
           <el-select v-model="formData.people" placeholder="请选择民族" style="width:100%" :clearable="true" >
              <el-option v-for="(item,key) in peopleOptions" :key="key" :label="item.label" :value="item.value" />
           </el-select>
       </el-form-item>
        <el-form-item label="状态:" prop="status">
           <el-select v-model="formData.status" placeholder="请选择状态" style="width:100%" :clearable="true" >
              <el-option v-for="(item,key) in typeOptions" :key="key" :label="item.label" :value="item.value" />
           </el-select>
       </el-form-item>
        <el-form-item label="证件号码:" prop="cardNum">
          <el-input v-model="formData.cardNum" :clearable="true"  placeholder="请输入证件号码" />
       </el-form-item>
        <el-form-item label="婚姻情况:" prop="hyStatus">
           <el-select v-model="formData.hyStatus" placeholder="请选择婚姻情况" style="width:100%" :clearable="true" >
              <el-option v-for="(item,key) in hy_statusOptions" :key="key" :label="item.label" :value="item.value" />
           </el-select>
       </el-form-item>
        <el-form-item label="通讯地址:" prop="address">
          <el-input v-model="formData.address" :clearable="true"  placeholder="请输入通讯地址" />
       </el-form-item>
        <el-form-item label="操作人员:" prop="doctor">
          <el-input v-model="formData.doctor" :clearable="true"  placeholder="请输入操作人员" />
       </el-form-item>
        <el-form-item label="备注:" prop="menu">
          <el-input v-model="formData.menu" :clearable="true"  placeholder="请输入备注" />
       </el-form-item>
        <el-form-item>
          <el-button :loading="btnLoading" type="primary" @click="save">保存</el-button>
          <el-button type="primary" @click="back">返回</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script setup>
import {
  createPatient,
  updatePatient,
  findPatient
} from '@/api/patients/patient'

defineOptions({
    name: 'PatientForm'
})

// 自动获取字典
import { getDictFunc } from '@/utils/format'
import { useRoute, useRouter } from "vue-router"
import { ElMessage } from 'element-plus'
import { ref, reactive } from 'vue'


const route = useRoute()
const router = useRouter()

// 提交按钮loading
const btnLoading = ref(false)

const type = ref('')
const genderOptions = ref([])
const CardTypeOptions = ref([])
const peopleOptions = ref([])
const typeOptions = ref([])
const GeneticDiseaseOptions = ref([])
const hy_statusOptions = ref([])
const formData = ref({
            patientCardNum: undefined,
            name: '',
            sex: '',
            mobile: '',
            patientImage: '',
            haveHastile: '',
            cardType: '',
            borthid: new Date(),
            age: undefined,
            people: '',
            status: '',
            cardNum: '',
            hyStatus: '',
            address: '',
            doctor: '',
            menu: '',
        })
// 验证规则
const rule = reactive({
               patientCardNum : [{
                   required: true,
                   message: '',
                   trigger: ['input','blur'],
               }],
               name : [{
                   required: true,
                   message: '',
                   trigger: ['input','blur'],
               }],
               mobile : [{
                   required: true,
                   message: '',
                   trigger: ['input','blur'],
               }],
})

const elFormRef = ref()

// 初始化方法
const init = async () => {
 // 建议通过url传参获取目标数据ID 调用 find方法进行查询数据操作 从而决定本页面是create还是update 以下为id作为url参数示例
    if (route.query.id) {
      const res = await findPatient({ ID: route.query.id })
      if (res.code === 0) {
        formData.value = res.data
        type.value = 'update'
      }
    } else {
      type.value = 'create'
    }
    genderOptions.value = await getDictFunc('gender')
    CardTypeOptions.value = await getDictFunc('CardType')
    peopleOptions.value = await getDictFunc('people')
    typeOptions.value = await getDictFunc('type')
    GeneticDiseaseOptions.value = await getDictFunc('GeneticDisease')
    hy_statusOptions.value = await getDictFunc('hy_status')
}

init()
// 保存按钮
const save = async() => {
      btnLoading.value = true
      elFormRef.value?.validate( async (valid) => {
         if (!valid) return btnLoading.value = false
            let res
           switch (type.value) {
             case 'create':
               res = await createPatient(formData.value)
               break
             case 'update':
               res = await updatePatient(formData.value)
               break
             default:
               res = await createPatient(formData.value)
               break
           }
           btnLoading.value = false
           if (res.code === 0) {
             ElMessage({
               type: 'success',
               message: '创建/更改成功'
             })
           }
       })
}

// 返回按钮
const back = () => {
    router.go(-1)
}

</script>

<style>
</style>
